//
//  MZDetailViewController.m
//  民族文化宫
//
//  Created by kobehjk on 14-8-27.
//  Copyright (c) 2014年 WANG CUNRUI. All rights reserved.
//

#import "MZDCDetailViewController.h"
#import "SBJson.h"

@interface MZDCDetailViewController ()<UIAlertViewDelegate>

@property (strong,nonatomic) NSString *imagePath;
@property (strong, nonatomic) NSArray *currentdata;
@property (nonatomic) NSInteger countp;
@property (strong,nonatomic) NSDictionary *dice;

@end

@implementation MZDCDetailViewController
@synthesize leibie = _leibie;
@synthesize minzu = _minzu;
@synthesize pic = _pic;
@synthesize time = _time;
@synthesize content_c = _content_c;
@synthesize content_e = _content_e;
@synthesize diqu = _diqu;
@synthesize zhidi = _zhidi;
@synthesize name = _name;
@synthesize scrollView = _scrollView;
@synthesize wenwuID = _wenwuID;



- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)headerRereshing
{
    NSLog(@"%@",_wenwuID);
    NSString *urlString= [NSString stringWithFormat:@"http://210.30.0.167:6162/mzserver/Home/getCpinfoByID?cp_id= %@",_wenwuID];
    NSLog(@"%@",urlString);
    
    
    NSString * encodedString = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes( kCFAllocatorDefault, (CFStringRef)urlString, NULL, NULL,  kCFStringEncodingUTF8 ));
    
    NSURL *urls =[NSURL URLWithString:encodedString];
    NSLog(@"%@",urls);
    
    ASIHTTPRequest *request = [[ASIHTTPRequest alloc] initWithURL:urls];
    //request.delegate = self;
    [request startSynchronous];
    NSLog(@"下载产品数据中");
    NSError *error = [request error];
    if (!error) {
        _currentdata = [[request responseData]JSONValue] ;
        NSLog(@"_currentdata =%@",_currentdata);

    }

    

}

- (void)requestFinished:(ASIHTTPRequest *)request
{
    NSLog(@"成功");
}

- (void)requestFailed:(ASIHTTPRequest *)request
{
    UIAlertView *result = [[UIAlertView alloc] initWithTitle:@"提示" message:@"下载失败" delegate:self cancelButtonTitle:@"是" otherButtonTitles:@"否", nil];
    [result show];
    NSLog(@"失败");
}

- (void)loadImageAsyncWithIndexPath
{
    
    NSDictionary *dic = _currentdata[0];
    NSLog(@"_currentdata = %@",_currentdata);
    NSString *pic = [NSString stringWithFormat:@"%@", [dic objectForKey:@"cp_wenwutu"]];
    NSLog(@"pic = %@",pic);
    NSRange foundObj = [pic rangeOfString :@";" options:NSCaseInsensitiveSearch];
    if(foundObj.length>0)
    {
        NSRange range = [pic rangeOfString:@";"];
        NSString *b = [pic substringToIndex:range.location];
        NSLog(@"b = %@",b);
        NSString *imagePath = [NSString stringWithFormat:@"http://www.mzdigital.cn/%@", b];

        NSLog(@"imagepath = %@",imagePath);
        _imagePath = imagePath;
    }else
    {
        NSString *imagePath = [NSString stringWithFormat:@"http://www.mzdigital.cn/%@", pic];

        NSLog(@"imagepath%@",imagePath);
        _imagePath = imagePath;
    }
    //检测地址是否含有两张图片，有则取分号前一张
    UIImageView *moImage = [[UIImageView alloc] initWithImage: self.cacheImage];
    [moImage setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",_imagePath]] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
    moImage.frame = CGRectMake(0, 0, 320,400);
    [_scrollView addSubview:moImage];
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    [self headerRereshing];
    
    self.edgesForExtendedLayout = UIRectEdgeNone;
    CGRect frame = self.view.bounds;
    CGSize f = frame.size;
    CGFloat h = f.height;
    CGFloat w = f.width;
    _scrollView =[[UIScrollView alloc]initWithFrame:CGRectMake(0,0,w,h-114)];
    //    _scrollView =[[UIScrollView alloc]initWithFrame:CGRectMake(0,0,350,456)];
    _scrollView.backgroundColor=[UIColor colorWithRed:0.9373 green:0.9216 blue:0.9059 alpha:1.0];
    _scrollView.pagingEnabled=YES;
    [self.view addSubview:_scrollView];
    NSDictionary *dic = _currentdata[0];
    [self loadImageAsyncWithIndexPath];
    _label1 = [[UILabel alloc]initWithFrame:CGRectMake(0, 400, 0, 0)];
    _label1.backgroundColor = [UIColor grayColor];
    [_label1 setNumberOfLines:0];
    _label1.lineBreakMode = NSLineBreakByWordWrapping;
    NSString *s = [NSString stringWithFormat:@"%@   %@",[dic objectForKey:@"cp_name"],[dic objectForKey:@"cp_shidai"]];
    _label1.text = s;
    
    CGSize size = CGSizeMake(320,200);
    NSDictionary *attribute = @{NSFontAttributeName: _label1.font};
    CGSize labelsize = [_label1.text boundingRectWithSize:size options: NSStringDrawingTruncatesLastVisibleLine |NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attribute context:nil].size;
    _label1.frame = CGRectMake(0,400, 320, labelsize.height);
    
    [_scrollView addSubview:_label1];
    _label2 = [[UILabel alloc] initWithFrame:CGRectMake( 10, 440, 300, 20)];
    _label2.text = [NSString stringWithFormat:@"民族 ：%@     质地 ：%@ ",[dic objectForKey:@"cp_minzu"],[dic objectForKey:@"cp_zhidi"]];
    _label2.font = [UIFont boldSystemFontOfSize:15];
    
    [_scrollView addSubview:_label2];
    _label3 = [[UILabel alloc] initWithFrame:CGRectMake( 10, 470, 300, 20)];
    _label3.text = [NSString stringWithFormat:@"地区 ：%@",[dic objectForKey:@"cp_diqu"]];
    _label3.font = [UIFont boldSystemFontOfSize:15];
    
    [_scrollView addSubview:_label3];
    _label4 = [[UILabel alloc] initWithFrame:CGRectMake( 10, 500, 300, 20)];
    _label4.text = [NSString stringWithFormat:@"类别 ：%@",[dic objectForKey:@"cp_leibie"]];
    _label4.font = [UIFont boldSystemFontOfSize:15];
    [_scrollView addSubview:_label4];
    
    _label5 = [[UILabel alloc] initWithFrame:CGRectMake( 10, 530, 300, 20)];
    _label5.text = [NSString stringWithFormat:@"尺寸 ：%@",[dic objectForKey:@"cp_chicun"]];
    _label5.font = [UIFont boldSystemFontOfSize:15];
    [_scrollView addSubview:_label5];
    
    _label6 = [[UILabel alloc]initWithFrame:CGRectMake(0, 560, 0, 0)];
    [_label6 setNumberOfLines:0];
    _label6.lineBreakMode = NSLineBreakByWordWrapping;
    NSString *s1 = [NSString stringWithFormat:@"说明：%@",[dic objectForKey:@"cp_shuoming"]];
    _label6.text = s1;
    UIFont *font = [UIFont boldSystemFontOfSize:15];
    _label6.font = font;
    CGSize size1 = CGSizeMake(320,500);
    NSDictionary *attribute1 = @{NSFontAttributeName: _label6.font};
    CGSize labelsize1 = [_label6.text boundingRectWithSize:size1 options: NSStringDrawingTruncatesLastVisibleLine |NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attribute1 context:nil].size;
    _label6.frame = CGRectMake(10,560, 300, labelsize1.height);
    
    int y = CGRectGetMaxY(_label6.frame);
    [_scrollView setContentSize:(CGSizeMake(CGRectGetWidth(_scrollView.frame), y))];
    
    [_scrollView addSubview:_label6];
    
}



- (void)makeViewfalus
{
    UIAlertView *alert = [[UIAlertView alloc]
                          initWithTitle:@"提示"
                          message:@"二维码错误"
                          delegate:self
                          cancelButtonTitle:@"确定"
                          otherButtonTitles:@"取消",nil];
    [alert show];

}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
//        MZAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
//        [appDelegate.myCache clearCachedResponsesForStoragePolicy:ASICachePermanentlyCacheStoragePolicy];
        
    }
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
